'use strict';

angular.module('Query').controller('ExceptionController', ['$scope', 'BaseModule', 'BaseEnum', 'BaseFunction', 'ExtendEnumService', 'BarcodeEnum', '$location', 'Authentication',
    function($scope, BaseModule, BaseEnum, BaseFunction, ExtendEnumService, BarcodeEnum, $location, Authentication) {
        BaseModule.initModule($scope, 'ProcessException');
        $scope.Module.ProcessException.LoadMode = BaseEnum.LoadMode.CacheAndPaging;
        $scope.Module.ProcessException.Condition = {IsRead: {$ne: true}};
        $scope.Module.ProcessException.OrderBy = {MissingWorkDateTime: -1};
        $scope.Module.ProcessException.PageSize = 15;

        $scope.QCStatusEnum;
        ExtendEnumService.getExtendEnum('QCStatus', function(extendEnum) {
            $scope.QCStatusEnum = extendEnum;
        });
        $scope.ExceptionTypeEnum = BaseFunction.getEnum('BarcodeEnum.ExceptionType');

        $scope.Department = {
            _id: null,
            Code: '',
            Name: ''
        };

        $scope.departmentChange = function(records){
            if(records.length > 0){
                $scope.Module.ProcessException.Condition.DepartmentName = records[0].Name;
                $scope.Module.ProcessException.load();
            }
            else{
                delete $scope.Module.ProcessException.Condition.DepartmentName;
                $scope.Module.ProcessException.load();
            }
        };
        
        $scope.btnHaveRead = function(){
            var selectedRecords = $scope.Module.ProcessException.SelectedRecords;

            if(selectedRecords.length > 0){
                for(var i=0; i<selectedRecords.length; i++){
                    selectedRecords[i].RowStatus = BaseEnum.RowStatus.Modify.Value;
                    selectedRecords[i].IsRead = true;
                }

                $scope.Module.ProcessException.save();
            }
        };
    }
]);